package main

import (
	"fmt"
	"github.com/gorilla/websocket"
	"github.com/liu-junyong/go-logger/logger"
	. "github.com/liu-junyong/golinuxhelper"
	"net/http"

	. "./session"
	. "./configs"
	"flag"
	"github.com/256dpi/gomqtt/broker"
	"github.com/256dpi/gomqtt/transport"

	"os"
	"os/signal"
	"reflect"
	"runtime"
	"syscall"
)

type Head struct {
	Cmd  int32
	Flag int32
}



var upgrader = websocket.Upgrader{
	CheckOrigin:     func(r *http.Request) bool { return true },
	ReadBufferSize:  1024 * 16,
	WriteBufferSize: 1024 * 16,
	Subprotocols:    []string{"mqtt", "mqtt3.1"},
} // use default options

var g_tcpManager *TcpManager
var g_playerManager *PlayerManager


func main() {

	localWS := flag.String("ws", ":7200", "--ws=:port")
	flag.Parse()

	go DoSig()

	runtime.GOMAXPROCS(runtime.NumCPU())
	logger.SetConsole(true)
	logger.SetRollingDaily("./log", "gate.log")
	logger.SetLevel(logger.ALL)

	x := new(Head)
	y := reflect.TypeOf(x)
	z := reflect.New(y).Interface()
	logger.Info(z)

	InitConfig()



	//cardNum := GetConfigManger().GetCardConfigs().GetCardUse(200,false,6,3)
	//logger.Info("测试好卡数量:", cardNum)

	g_tcpManager = &TcpManager{}
	g_playerManager = &PlayerManager{}

	GetConfigManger().Init(g_tcpManager)

	g_tcpManager.Init(g_playerManager)
	g_playerManager.Init(g_tcpManager)

	endpoint := fmt.Sprintf("ws://127.0.0.1%s", *localWS)
	fmt.Println(endpoint)
	server, err := transport.Launch(endpoint)
	if err != nil {
		panic(err)
	}

	backend := NewMyBackend()
	backend.Init(g_playerManager)
	engine := broker.NewEngine(backend)
	engine.Accept(server)

	ch := make(chan os.Signal, 1)
	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
	sig := <-ch
	logger.Info("Received signal", "signal", sig.String())
}

